package com.tanhua.server.service;

import com.tanhua.commons.template.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.ExploreVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.ExploreApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.utils.ShuffleUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 左滑右滑业务层
 */
@Service()
public class ExploreService {
    
    //调用用户详细信息api
    @Reference
    private UserInfoApi userInfoApi;
    
    //注入redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    //调用朋友api
    @Reference
    private FriendApi friendApi;
    
    //调用滑动次数api
    @Reference
    private ExploreApi exploreApi;
    
    //调用用户喜欢api
    @Reference
    private UserLikeApi userLikeApi;
    
    @Autowired
    private ShuffleUtils shuffleUtils;
    
    //注入环信
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    
    //探花喜欢的key
    private static final String EXPLORE_LOVE_ = "EXPLORE_LOVE_";
    
    /**
     * 探花-左滑右滑
     * GET/tanhua/cards
     *
     * @return
     */
    public ResponseEntity<Object> explore() throws InvocationTargetException, IllegalAccessException {
        //从本地线程里获取用户id
        Long userId = UserHolder.getUserId();
        //调用工具类获取随机用户集合
        List<Long> shuffle = shuffleUtils.shuffle(userId);
        
        //创建一个vo集合用来封装返回信息
        List<ExploreVo> voList = new ArrayList<>();
        //遍历集合
        for (Long id : shuffle) {
            //创建一个vo对象
            ExploreVo exploreVo = new ExploreVo();
            //调用api查询用户详细信息
            UserInfo userInfo = userInfoApi.findById(id);
            //判断详细信息是否为空
            if (userInfo != null) {
                //调用工具复制
                BeanUtils.copyProperties(userInfo, exploreVo);
                //判断标签是否为空
                if (userInfo.getTags() != null) {
                    //标签
                    exploreVo.setTags(userInfo.getTags().split(","));
                }
            }
            //添加到集合里
            voList.add(exploreVo);
        }
        
        //返回
        return ResponseEntity.ok(voList);
    }
    
    /**
     * 探花-喜欢
     *
     * @param loveId 喜欢的用户id
     * @return
     */
    public ResponseEntity<Object> love(Long loveId) {
        //从本地线程里获取用户id
        Long userId = UserHolder.getUserId();
        
        //调用api查询用户的滑动次数
        Long count = exploreApi.queryUserCount(userId, "explore");
        //判断滑动次数是否到100
        if ((100 - count) <= 0) {
            //到100提示用户
            return ResponseEntity.status(500).body("滑动次数已到上限，请明天再来");
        }
        
        //把喜欢的记录存放到redis里
        String key = EXPLORE_LOVE_ + userId + "_" + loveId;
        //设置时限
        redisTemplate.opsForValue().set(key, "1");
        //调用api保存到喜欢表里
        userLikeApi.save(userId, loveId);
        
        //到redis里寻找对方是否喜欢你
        String loveKey = EXPLORE_LOVE_ + loveId + "_" + userId;
        if (redisTemplate.hasKey(loveKey)) {
            //有这个key，说明对方也喜欢你了
            //先删除记录
            redisTemplate.delete(key);
            redisTemplate.delete(loveKey);
            
            //调用api添加对方为好友,同时添加环信的
            friendApi.addContacts(userId, loveId);
            huanXinTemplate.contactUsers(userId, loveId);
            //删除粉丝表里的数据
            userLikeApi.remove(userId, loveId);
            userLikeApi.remove(loveId, userId);
            //发送消息通知双方
            //huanXinTemplate.sendMsg(userId.toString(),);
            //返回
            return ResponseEntity.ok(null);
        }
        //返回
        return ResponseEntity.ok(null);
    }
    
    /**
     * 探花-不喜欢
     *
     * @param unLoveId 不喜欢的用户id
     * @return
     */
    public ResponseEntity<Object> unLove(Long unLoveId) {
        //从本地线程获取用户id
        Long userId = UserHolder.getUserId();
        
        //调用api查询用户的滑动次数
        Long count = exploreApi.queryUserCount(userId, "explore");
        //判断滑动次数是否到100
        if ((100 - count) <= 0) {
            //到0提示用户
            return ResponseEntity.status(500).body("滑动次数已到上限，请明天再来");
        }
        
        //查询之前是否有喜欢过此用户
        String key = EXPLORE_LOVE_ + userId + "_" + unLoveId;
        //判断
        if (redisTemplate.hasKey(key)) {
            //喜欢过则删过记录
            redisTemplate.delete(key);
            //调用api删除喜欢关系
            userLikeApi.remove(userId, unLoveId);
        }
        
        //返回
        return ResponseEntity.ok(null);
    }
}
